package com.zbu.system.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbu.common.core.constants.Constants;
import com.zbu.common.core.domain.R;
import com.zbu.common.core.domain.TableDataInfo;
import com.zbu.common.core.enums.ResultCode;
import com.zbu.common.security.exception.ServiceException;
import com.zbu.system.domain.question.Question;
import com.zbu.system.domain.question.dto.QuestionAddDTO;
import com.zbu.system.domain.question.dto.QuestionEditDTO;
import com.zbu.system.domain.question.dto.QuestionQueryDto;
import com.zbu.system.domain.question.es.QuestionES;
import com.zbu.system.domain.question.vo.QuestionDetailVO;
import com.zbu.system.domain.question.vo.QuestionVO;
import com.zbu.system.elasticsearch.QuestionRepository;
import com.zbu.system.manager.QuestionCacheManager;
import com.zbu.system.mapper.question.QuestionMapper;
import com.zbu.system.service.question.IQuestionService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Resource
    QuestionMapper questionMapper;
    @Autowired
    QuestionRepository questionRepository;
    @Autowired
    QuestionCacheManager questionCacheManager;
    @Override
    public List<QuestionVO> list(QuestionQueryDto questionQueryDto) {
        String excludeIdStr=questionQueryDto.getExcludeIdStr();
        if(StrUtil.isNotEmpty(excludeIdStr)){
            String[] excludeIdArr=excludeIdStr.split(Constants.SPLIT_SEM);
            Set<Long> excludeIdSet= Arrays.stream(excludeIdArr)
                    .map(Long::valueOf)
                    .collect(Collectors.toSet());
            questionQueryDto.setExcludeIdSet(excludeIdSet);
        }

        PageHelper.startPage(questionQueryDto.getPageNum(), questionQueryDto.getPageSize());
        List<QuestionVO> questions = questionMapper.selectQuestionList(questionQueryDto);
        PageInfo<QuestionVO> pageInfo = new PageInfo<>(questions);
        return pageInfo.getList();
    }

    @Override
    public boolean  add(QuestionAddDTO questionAddDTO) {
        List<Question>questions=questionMapper.selectList(new LambdaQueryWrapper<Question>().
                eq(Question::getTitle,questionAddDTO.getTitle()));
        if(CollectionUtil.isNotEmpty(questions)){
            throw  new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question=new Question();

        BeanUtil.copyProperties(questionAddDTO,question);
        int insert =questionMapper.insert(question);
        if(insert<=0){
             return false;
        }
        QuestionES questionES=new QuestionES();
        BeanUtil.copyProperties(question,questionES);
        questionCacheManager.addCache(question.getQuestionId());
        questionRepository.save(questionES);
        return true;
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question=questionMapper.selectById(questionId);
        if(question==null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        QuestionDetailVO questionDetailVO=new QuestionDetailVO();
        BeanUtil.copyProperties(question,questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        Question oldQuestion=questionMapper.selectById(questionEditDTO.getQuestionId());
        if(oldQuestion==null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        oldQuestion.setQuestionId(questionEditDTO.getQuestionId());
        oldQuestion.setTitle(questionEditDTO.getTitle());
        oldQuestion.setDifficulty(questionEditDTO.getDifficulty());
        oldQuestion.setTimeLimit(questionEditDTO.getTimeLimit());
        oldQuestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
        oldQuestion.setContent(questionEditDTO.getContent());
        oldQuestion.setQuestionCase(questionEditDTO.getQuestionCase());
        oldQuestion.setDefaultCode(questionEditDTO.getDefaultCode());
        oldQuestion.setMainFuc(questionEditDTO.getMainFuc());
        QuestionDetailVO questionDetailVO=new QuestionDetailVO();
        QuestionES questionES=new QuestionES();
        BeanUtil.copyProperties(oldQuestion,questionES);
        questionRepository.save(questionES);
        return questionMapper.updateById(oldQuestion);
    }

    @Override
    public int delete(Long questionId) {
        Question question=questionMapper.selectById(questionId);
        if(question==null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        questionRepository.deleteById(questionId);
        questionCacheManager.deleteCache(questionId);
        return questionMapper.deleteById(questionId);
    }
}
